package com.hotleproject.controller;

import com.hotleproject.pojo.Room;
import com.hotleproject.service.AdminiOperaService;
import com.hotleproject.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/users")
public class RoomController {
    @Autowired
    private RoomService roomService;

    //    @RequestMapping("/findRoomAll")
//    public String findUserAll(Model model){
//        List<Room> list=this.roomService.findAll();
//        model.addAttribute("list",list);
//        return  "room_data";
//    }
    @RequestMapping("/findRoomPage")
    public String findRoomPage(HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 5;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = roomService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Room> list = this.roomService.pageList(tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "room_data";
    }


    @RequestMapping("/findSingleRoomPage")
    public String findSingleRoomPage(HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 5;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = roomService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Room> list = this.roomService.pageList(tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "Singleroom_data";
    }

    @Autowired
    private AdminiOperaService adminiOperaService;

    @RequestMapping("/addRoom")
    public String addRoom(@RequestParam("roomImg") MultipartFile file, HttpSession session, HttpServletRequest request, Model model) throws IOException {

        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "E:/IntelliJ IDEA 2018.3.5/hotleproject/src/main/resources/static/manage/images/";
        File dest = new File(filePath + fileName);
        String name = filePath + fileName;
        try {
            file.transferTo(dest);
            this.roomService.addRoom(request.getParameter("roomType"),
                    request.getParameter("roomPrice"),
                    fileName, request.getParameter("roomBed"), request.getParameter("roomFaci"), request.getParameter("roomNum"));
            ;

            ////日志操作

            String roomType = request.getParameter("roomType");
            String roomPrice = request.getParameter("roomPrice");
            String roomBed = request.getParameter("roomBed");
            String roomFaci = request.getParameter("roomFaci");
            String roomNum = request.getParameter("roomNum");
            String AdminiName = session.getAttribute("adminis").toString();
            String operationThing = "管理员" + AdminiName + "添加了房型：" + roomType
                    + "。房型价格：" + roomPrice + "。房型描述：" + roomFaci + "。床型大小：" + roomBed + "。" + "房间数量：" + roomNum;
//        String operationDate=request.getParameter("operationDate");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String operationDate = df.format(new Date());

            this.adminiOperaService.insertope(AdminiName, operationThing, operationDate);

            return "redirect:/users/findRoomPage";
//            return "text";
        } catch (IOException e) {
//            LOGGER.error(e);

        }
        return "redirect:/users/findRoomPage";
    }
}
